﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using nMongo.Tests.Entities;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace nMongo.Tests.Repository
{
    public partial class MongoRepositoryTests
    {
        [TestMethod]
        public void DeleteWithSingleObject()
        {
            _MongoRepositoryTestsPrepare.RevertAnyChanges();

            MongoRepository<User> users = new MongoRepository<User>(TestsConfiguration.ConnectionString, TestsConfiguration.DatabaseName);

            User user1 = MockGenerator.GenerateOne<User>();
            User user2 = MockGenerator.GenerateOne<User>();
            User user3 = MockGenerator.GenerateOne<User>();
            users.Insert(user1);
            users.Insert(user2);
            users.Insert(user3);

            // Assert

            // Delete 1st overload
            Assert.IsTrue(users.Delete(user1));

            // Delete 2nd overload
            WriteConcernResult result2;
            Assert.IsTrue(users.Delete(user2, out result2));
            Assert.IsTrue(result2.Ok);

            // Delete 3rd overload
            WriteConcernResult result3;
            Assert.IsTrue(users.Delete(user3, WriteConcern.Acknowledged, out result3));
            Assert.IsTrue(result3.Ok);
        }

        [TestMethod]
        public void DeleteWithMultipleObjects()
        {
            _MongoRepositoryTestsPrepare.RevertAnyChanges();

            MongoRepository<User> users = new MongoRepository<User>(TestsConfiguration.ConnectionString, TestsConfiguration.DatabaseName);

            IEnumerable<User> users1 = MockGenerator.GenerateList<User>(10);
            IEnumerable<User> users2 = MockGenerator.GenerateList<User>(20);
            IEnumerable<User> users3 = MockGenerator.GenerateList<User>(30);
            users.Insert(users1);
            users.Insert(users2);
            users.Insert(users3);

            // Assert

            // Delete 1st overload
            Assert.AreEqual(users.Delete(users1), 10);

            // Delete 2nd overload
            IDictionary<User, WriteConcernResult> results2;
            Assert.AreEqual(users.Delete(users2, out results2), 20);
            Assert.IsFalse(results2.Any(x => !x.Value.Ok));

            // Delete 3rd overload
            IDictionary<User, WriteConcernResult> results3;
            Assert.AreEqual(users.Delete(users3, out results3), 30);
            Assert.IsFalse(results3.Any(x => !x.Value.Ok));
        }
    }
}
